package com.xlh.util;

import cn.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;

/**
 * @author chufangzheng
 * excel 公共样式的抽取
 */
public class CellStyleUtils {


    /**
     * 普通cell的样式创建
     * @param workbook
     * @return
     */
    public static CellStyle createCellStyle(HSSFWorkbook workbook) {

        CellStyle headerCellStyle = workbook.createCellStyle();
        addFullBorder(headerCellStyle);

        return headerCellStyle;
    }

    /**
     * 创建所有边框
     * @param headerCellStyle
     */
    public static void addFullBorder(CellStyle headerCellStyle) {
        headerCellStyle.setBorderBottom(BorderStyle.THIN);
        headerCellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        headerCellStyle.setBorderLeft(BorderStyle.THIN);
        headerCellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        headerCellStyle.setBorderRight(BorderStyle.THIN);
        headerCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        headerCellStyle.setBorderTop(BorderStyle.THIN);
        headerCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
    }


    /**
     * 通用头部样式
     * @param workbook
     * @return
     */
    public static CellStyle universalTitleStyle(HSSFWorkbook workbook) {

        HSSFPalette palette = workbook.getCustomPalette();
        // 设置主标题样式
        CellStyle titleStyle = workbook.createCellStyle();
        titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        titleStyle.setAlignment(HorizontalAlignment.LEFT);
        titleStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
        titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        palette.setColorAtIndex(HSSFColor.BLUE_GREY.index, (byte) 54, (byte) 96, (byte) 146);
        // 设置字体
        Font font = StyleUtil.createFont(workbook, IndexedColors.WHITE.index, (short) 12, "宋体");
        font.setBold(true);
        titleStyle.setFont(font);
        return titleStyle;
    }


    /**
     * 创建头样式
     * @param workbook
     * @return
     */
    public static CellStyle createHeaderCellStyle(HSSFWorkbook workbook) {

        CellStyle headerCellStyle = workbook.createCellStyle();

        addFullBorder(headerCellStyle);
        // 设置背景色为蓝色
        HSSFPalette palette = workbook.getCustomPalette();
        HSSFColor hssfColor = palette.findSimilarColor((byte) 155, (byte) 194, (byte) 230);  // 深蓝

        //solid 填充  foreground  前景色
        headerCellStyle.setFillForegroundColor(hssfColor.getIndex());
        headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        return headerCellStyle;
    }


    /**
     * 填充样式为浅蓝色
     * @param workbook
     * @return
     */
    public static CellStyle creadeSecondHeaderStyle(HSSFWorkbook workbook){

        CellStyle headerCellStyle = workbook.createCellStyle();
        addFullBorder(headerCellStyle);

        HSSFPalette palette = workbook.getCustomPalette();
        palette.setColorAtIndex((short) 11,(byte) 197, (byte) 217, (byte) 241);  // 浅蓝色
        //solid 填充  foreground  前景色
        headerCellStyle.setFillForegroundColor(palette.getColor((short) 11).getIndex());
        headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        return headerCellStyle;
    }
}
